package edu.ptit.jad.mysql;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import org.jsoup.Jsoup;

import vn.hus.nlp.tokenizer.VietTokenizer;

public class MySqlDb {

	public static void main(String[] args) throws Exception {

		Class.forName("com.mysql.jdbc.Driver");

		Connection m_Connection = DriverManager.getConnection(
				"jdbc:mysql://192.168.3.87:3306/adtech_banner?autoReconnect=true"
						+ "&zeroDateTimeBehavior=convertToNull", "datlt",
				"dat123456");

		Statement m_Statement = m_Connection.createStatement();
		String query = "SELECT  A.title, A.description, A.keyword, A.content   FROM `ad_banner` AS A LIMIT 10";

		ResultSet m_ResultSet = m_Statement.executeQuery(query);

		File file = new File("data/trains/ads_landingpage.jad");
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(file, true), "UTF-8"));

		VietTokenizer tokenizer = new VietTokenizer();

		// logger.info(banner.getBannerId() + banner.getTitle()

		int top = 30000;
		int current = 0;

		System.out.println("Estimate " + top + " records");
		System.out.println("");
		for (int i = 0; i < 100; i++) {
			System.out.print("=");
		}
		System.out.println("");

		String text = "";
		while (m_ResultSet.next()) {

			
			text += Jsoup.parse(m_ResultSet.getString("title")).text();
			text += Jsoup.parse(m_ResultSet.getString("description")).text();
			text += Jsoup.parse(m_ResultSet.getString("keyword")).text();
			text += Jsoup.parse(m_ResultSet.getString("content")).text();

			String[] tokens = tokenizer.tokenize(text);

			for (String token : tokens) {

				out.write(token + " ");

			}

			out.write("\n");

			current++;

			double percent = (double) current * 100 / top;

			if (percent - Math.round(percent) == 0) {

				System.err.print("=");

			}

		}

		System.out.println("");

		System.out.println("Done!");
		out.close();
	}
}
